package com.tky.conference.implInterfaces;

import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.tky.conference.base.UrlConstant;
import com.tky.conference.entity.FailRequest;
import com.tky.conference.entity.LoginOutInfo;
import com.tky.conference.interfaces.DeleteInter;
import com.tky.conference.interfaces.HttpResultListener;

import org.apache.http.Header;
import org.json.JSONObject;

/**
 * Created by cll on 2015/12/16.
 */
public class DeleteImpl implements DeleteInter{
    private String TAG = "DeleteImpl";
    private LoginOutInfo loginOutInfobj;
    private FailRequest failRequestobj;
    private HttpResultListener httpResultListener;

    public DeleteImpl(HttpResultListener httpResultListener) {
        this.httpResultListener = httpResultListener;
    }
    @Override
    public void delete(String id) {
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("id", id);
        client.setTimeout(UrlConstant.TIME_OUT);
        client.post(
                UrlConstant.DELETE,
                params,
                new AsyncHttpResponseHandler() {
                    @Override
                    public void onFailure(int arg0, Header[] arg1, byte[] arg2,
                                          Throwable arg3) {
                        System.out.println("fail");
                        try {
                            System.out.println("fail");
                            try {
                                failRequestobj = new FailRequest();
                                failRequestobj.setStatus("-1");
                                failRequestobj.setMsg("网络异常");
                                httpResultListener.onResult(failRequestobj);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                        System.out.println("success");
                        Log.i(TAG, new String(arg2));
                        try {
                            JSONObject jsonObject=new JSONObject(new String(arg2));
                            loginOutInfobj=new LoginOutInfo();
                            loginOutInfobj.setStatus(jsonObject.getString("status"));
                            loginOutInfobj.setMsg(jsonObject.getString("msg"));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        httpResultListener.onResult(loginOutInfobj);
                    }
                    @Override
                    public void onStart() {
                        super.onStart();
                        httpResultListener.onStartRequest();
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        httpResultListener.onFinish();
                    }
                }
        );
    }
}
